<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { userApi } from '../api'

const router = useRouter()

// 当前用户信息
const currentUser = ref(null)
// 侧边栏折叠状态
const sidebarCollapsed = ref(false)

// 获取当前用户信息
const fetchCurrentUser = async () => {
  try {
    // 先从localStorage获取用户信息
    const userStr = localStorage.getItem('user')
    if (userStr) {
      currentUser.value = JSON.parse(userStr)
      console.log('从localStorage获取的用户信息:', currentUser.value)
    }
    
    // 然后从服务器获取最新的用户信息
    const res = await userApi.getCurrentUser()
    if (res && res.data) {
      currentUser.value = res.data
      // 更新localStorage中的用户信息
      localStorage.setItem('user', JSON.stringify(currentUser.value))
      console.log('从服务器获取的用户信息:', currentUser.value)
    } else {
      console.error('获取用户信息失败: 服务器返回空数据')
    }
  } catch (error) {
    console.error('获取用户信息失败:', error)
    // 如果获取用户信息失败，可能是未登录或会话过期
    // 但如果本地已有用户信息，则继续使用本地信息
    if (!currentUser.value) {
      // 如果本地也没有用户信息，则跳转到登录页
      router.push('/login')
    }
  }
}

// 退出登录
const handleLogout = async () => {
  try {
    await userApi.logout()
    // 清除本地存储的用户信息
    localStorage.removeItem('user')
    // 跳转到登录页
    router.push('/login')
  } catch (error) {
    console.error('退出登录失败:', error)
  }
}

// 切换侧边栏折叠状态
const toggleSidebar = () => {
  sidebarCollapsed.value = !sidebarCollapsed.value
}

// 组件挂载时获取用户信息
onMounted(() => {
  fetchCurrentUser()
})
</script>

<template>
  <div class="dashboard-container">
    <!-- 顶部导航栏 -->
    <header class="header">
      <div class="header-left">
        <button @click="toggleSidebar" class="toggle-btn">
          <span class="toggle-icon">☰</span>
        </button>
        <h1 class="app-title">二维码管理系统</h1>
      </div>
      
      <div class="header-right" v-if="currentUser">
        <span class="user-info">{{ currentUser.username }}</span>
        <button @click="handleLogout" class="logout-btn">退出登录</button>
      </div>
    </header>
    
    <div class="dashboard-content">
      <!-- 侧边栏导航 -->
      <aside class="sidebar" :class="{ 'collapsed': sidebarCollapsed }">
        <nav class="nav-menu">
          <router-link to="/dashboard/qrcode" class="nav-item">
            <span class="nav-icon">🔍</span>
            <span class="nav-text" v-show="!sidebarCollapsed">我的二维码</span>
          </router-link>
          
          <router-link to="/dashboard/qrcode/create" class="nav-item">
            <span class="nav-icon">➕</span>
            <span class="nav-text" v-show="!sidebarCollapsed">创建二维码</span>
          </router-link>
          
          <router-link to="/dashboard/decode" class="nav-item">
            <span class="nav-icon">🔍</span>
            <span class="nav-text" v-show="!sidebarCollapsed">二维码解码</span>
          </router-link>
          
          <router-link to="/dashboard/category" class="nav-item">
            <span class="nav-icon">📂</span>
            <span class="nav-text" v-show="!sidebarCollapsed">分类管理</span>
          </router-link>
          
          <router-link to="/dashboard/statistics" class="nav-item">
            <span class="nav-icon">📊</span>
            <span class="nav-text" v-show="!sidebarCollapsed">数据统计</span>
          </router-link>
          
          <router-link v-if="currentUser && currentUser.role === 'admin'" to="/dashboard/user" class="nav-item">
            <span class="nav-icon">👥</span>
            <span class="nav-text" v-show="!sidebarCollapsed">用户管理</span>
          </router-link>
        </nav>
      </aside>
      
      <!-- 主内容区域 -->
      <main class="main-content">
        <router-view />
      </main>
    </div>
  </div>
</template>

<style scoped>
.dashboard-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background-color: #f5f5f5;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 60px;
  background-color: #4CAF50;
  color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.header-left {
  display: flex;
  align-items: center;
}

.toggle-btn {
  background: none;
  border: none;
  color: white;
  font-size: 20px;
  cursor: pointer;
  margin-right: 15px;
  padding: 5px;
}

.app-title {
  margin: 0;
  font-size: 1.5rem;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  margin-right: 15px;
}

.logout-btn {
  background-color: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
  padding: 5px 10px;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.logout-btn:hover {
  background-color: rgba(255, 255, 255, 0.3);
}

.dashboard-content {
  display: flex;
  flex: 1;
}

.sidebar {
  width: 240px;
  background-color: #fff;
  box-shadow: 2px 0 5px rgba(0, 0, 0, 0.05);
  transition: width 0.3s;
  overflow-x: hidden;
}

.sidebar.collapsed {
  width: 60px;
}

.nav-menu {
  display: flex;
  flex-direction: column;
  padding: 20px 0;
}

.nav-item {
  display: flex;
  align-items: center;
  padding: 12px 20px;
  color: #333;
  text-decoration: none;
  transition: background-color 0.3s;
}

.nav-item:hover {
  background-color: #f0f0f0;
}

.nav-item.router-link-active {
  background-color: #e8f5e9;
  color: #4CAF50;
  border-left: 3px solid #4CAF50;
}

.nav-icon {
  font-size: 18px;
  margin-right: 15px;
  width: 20px;
  text-align: center;
}

.sidebar.collapsed .nav-icon {
  margin-right: 0;
}

.main-content {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
}
</style>